﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Colorize filter
    /// </summary>
    public class Colorize : BaseImageFilter
    {
        protected byte m_nRed = 10;
        protected byte m_nGreen = 10;
        protected byte m_nBlue = 10;

        /// <summary>
        /// default constructor
        /// </summary>
        public Colorize()
        {
        }

        /// <summary>
        /// specify channels directly
        /// </summary>
        /// <param name="_red"></param>
        /// <param name="_green"></param>
        /// <param name="_blue"></param>
        public Colorize(byte _red, byte _green, byte _blue)
        {
            m_nRed = _red;
            m_nGreen = _green;
            m_nBlue = _blue;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_colorize"></param>
        internal Colorize(Colorize _colorize)
        {
            this.m_nRed = _colorize.m_nRed;
            this.m_nGreen = _colorize.m_nGreen;
            this.m_nRed = _colorize.m_nRed;
        }

        /// <summary>
        /// Gets or sets color
        /// </summary>
        public Color Color
        {
            get { return Color.FromArgb(m_nRed, m_nGreen, m_nBlue); }
            set
            {
                m_nRed = value.R;
                m_nGreen = value.G;
                m_nBlue = value.B;
            }
        }

        /// <summary>
        /// Gets or sets red
        /// </summary>
        public byte Red
        {
            get { return m_nRed; }
            set { m_nRed = value; }
        }

        /// <summary>
        /// Gets or sets green
        /// </summary>
        public byte Green
        {
            get { return m_nGreen; }
            set { m_nGreen = value; }
        }

        /// <summary>
        /// Gets or sets blue
        /// </summary>
        public byte Blue
        {
            get { return m_nBlue; }
            set { m_nBlue = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            if (m_nRed < -255 || m_nRed > 255) return _bitmap;
            if (m_nGreen < -255 || m_nGreen > 255) return _bitmap;
            if (m_nBlue < -255 || m_nBlue > 255) return _bitmap;

            UnsafeBitmap bitmap = _bitmap;
            // GDI+ still lies to us - the return format is BGR, NOT RGB.
            BitmapData bmData = bitmap.BitmapData; //bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

            int stride = bmData.Stride;
            IntPtr Scan0 = bmData.Scan0;

            unsafe
            {
                byte* p = (byte*)(void*)Scan0;

                int nHeight = bitmap.Height;
                int nWidth = bitmap.Width;

                int nLineOffset = bitmap.OffsetPointerIncrease; //= stride - nWidth * 3;
                int nBytesPerPixelCount = bitmap.BytesPerPixelCount;
                int nPixel;

                for (int y = 0; y < nHeight; ++y)
                {
                    for (int x = 0; x < nWidth; ++x)
                    {
                        nPixel = p[2] + m_nRed;
                        nPixel = Math.Max(nPixel, 0);
                        p[2] = (byte)Math.Min(255, nPixel);

                        nPixel = p[1] + m_nGreen;
                        nPixel = Math.Max(nPixel, 0);
                        p[1] = (byte)Math.Min(255, nPixel);

                        nPixel = p[0] + m_nBlue;
                        nPixel = Math.Max(nPixel, 0);
                        p[0] = (byte)Math.Min(255, nPixel);

                        p += nBytesPerPixelCount;
                    }
                    p += nLineOffset;
                }
            }

            return _bitmap; //bitmap.UnlockBits(bmData);
        }

        /// <summary>
        /// Clones this filter
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Colorize(this);
        }
    }
}
